/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uvexposureapplication.client;

import java.util.Map;
import java.util.Observable;
import java.util.Observer;
import uvexposureapplication.eventrouting.Consumer;
import uvexposureapplication.eventrouting.elvin.ElvinConsumer;
import uvexposureapplication.eventrouting.elvin.notificationlistener.ServerNotificationListener;
import uvexposureapplication.rpc.AbstractPseudoRPCClientStub;
import uvexposureapplication.rpc.clients.ContextManagerPseudoRPCClientStub;
import uvexposureapplication.utils.IOStream;

/**
 *
 * @author matthieukluj
 */
public class UVAppUI implements Observer {

    private static Consumer consumer;
    private static String name;
    private String server;
    private static char option;
    public ServerNotificationListener ntf;
    public AbstractPseudoRPCClientStub contextManagerPseudoRPCClientStub;
    public AbstractPseudoRPCClientStub contextManagerPseudoRPCClientStub2;

    public UVAppUI(String server) {
        this.server = server;
        this.ntf = new ServerNotificationListener();
        this.ntf.addObserver(this);

        this.consumer = new ElvinConsumer(server, this.ntf);
    }

    /**·
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        if (args.length < 1) {
            System.out.println("usage: java UVAppUI [ElvinURL]");
            System.exit(0);
        }

        UVAppUI uvAppUI = new UVAppUI(args[0]);

        //consumer = new ElvinConsumer(args[0]);
        System.out.println("Location-aware UV Exposure Application");
        System.out.println("Please enter your user name:");
        name = IOStream.readLine();
        uvAppUI.consumer.subscribe("Group == 'ContextManager' && UserName == '" + name + "'");

        while (true) {

            System.out.println("Location-aware UV Exposure Application Main Menu");
            System.out.println("Please select an option:");
            System.out.println("1. Search for information on a specific item of interest");
            System.out.println("2. Search for items of interest in current location");
            System.out.println("E. Exit");

            option = IOStream.readLine().charAt(0);

            switch (option) {
                case '1':
                    System.out.println("Please enter name of item of interest:");
                    String item = IOStream.readLine();
                    uvAppUI.contextManagerPseudoRPCClientStub = new ContextManagerPseudoRPCClientStub(uvAppUI, "searchInfos", item, args[0]);
                    new Thread(uvAppUI.contextManagerPseudoRPCClientStub).start();
                    break;
                case '2':
                    uvAppUI.contextManagerPseudoRPCClientStub = new ContextManagerPseudoRPCClientStub(uvAppUI, "searchItemsInLocation", name, args[0]);
                    new Thread(uvAppUI.contextManagerPseudoRPCClientStub).start();
                    break;
                case 'E':
                    System.out.println("Bye bye");
                    uvAppUI.close();
                    break;
                default:
                    System.out.println("Enter a Valid option");
                    break;
            }

        }
    }

    public void printItemInformation(String item) {
        System.out.println("[RECEIVED] itemInformation FROM ContextManager");
        System.out.println(item);
    }

    public void printItemInLocation(String item) {
        System.out.println("[RECEIVED] item around location FROM ContextManager");
        System.out.println(item);
    }
    
    public void close() {
        AbstractPseudoRPCClientStub clientStub = new ContextManagerPseudoRPCClientStub(this, "shutDown", name, this.server);
        new Thread(clientStub).start();
        this.consumer.closeConnection();
        System.exit(1);
    }

    @Override
    public void update(Observable o, Object arg) {
        if (arg instanceof Map) {
            Map<String, String> argMap = (Map<String, String>) arg;
            if (argMap.get("Index").equals("fetchUserPreferenceUV")) {
                System.out.println("");
                System.out.println("Warning, Overexposure to UV, the current UVI is " + (String)argMap.get("IndexUV"));
                System.out.println("Suggestion - please go to " + (String)argMap.get("Value"));
                System.out.println("");
            } else if (argMap.get("Index").equals("fetchUserPreferenceTemp")) {
                System.out.println("");
                System.out.println("Warning, Temperature is now " + (String)argMap.get("IndexTemp"));
                System.out.println("Suggestion - please go to " + (String)argMap.get("Value"));
                System.out.println("");
            }
            System.out.println("Please select an option:");
            System.out.println("1. Search for information on a specific item of interest");
            System.out.println("2. Search for items of interest in current location");
            System.out.println("E. Exit");
        }
    }
}
